<template>
	<view class="page">
		<unicloud-db v-slot:default="{ data, loading, error, options }" collection="compare">
			<view v-if="error">{{ error.message }}</view>
			<view v-else-if="loading">正在加载...</view>
			<view v-else>
				<view class="score" v-for="(item, index) in getScoreData(data)" :key="item._id">
					<text>姓名: {{ item.username }}</text>
					<text>得分: {{ item.score }}</text>
				</view>
			</view>
		</unicloud-db>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { onLoad, onShow, onReady } from '@dcloudio/uni-app'

const getScoreData = data => {
	const obj = {}
	const arr = data
		.sort((a, b) => Number(b.score.slice(0, -1)) - Number(a.score.slice(0, -1)))
		.reduce((acc, c) => {
			obj[c.username] ? '' : (obj[c.username] = true && acc.push(c))
			return acc
		}, [])
	return arr
}

onLoad(() => {})
onReady(() => {})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
